package com.cmcc.linlaker.ui.mine.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cmcc.linlaker.R;
import com.cmcc.linlaker.application.LinhuApp;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.mine.bean.CheckRankingBean;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class RankingAdapter extends RecyclerView.Adapter<RankingAdapter.ViewHolder> {

    private List<CheckRankingBean.CheckRankingListBean> itemList;
    private Context mContext;

    public RankingAdapter(Context mContext, List<CheckRankingBean.CheckRankingListBean> itemList) {
        this.itemList = itemList;
        this.mContext = mContext;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.check_ranking_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        CheckRankingBean.CheckRankingListBean item = itemList.get(position);

        if (position == 0) {
            holder.image_ranking.setVisibility(View.VISIBLE);
            holder.image_ranking.setImageResource(R.mipmap.ranking_first_icon);
            holder.text_ranking1.setVisibility(View.VISIBLE);
            holder.text_ranking2.setVisibility(View.GONE);
            holder.text_ranking1.setText("1");
        } else if (position == 1) {
            holder.image_ranking.setVisibility(View.VISIBLE);
            holder.image_ranking.setImageResource(R.mipmap.ranking_second_icon);
            holder.text_ranking1.setVisibility(View.VISIBLE);
            holder.text_ranking2.setVisibility(View.GONE);
            holder.text_ranking1.setText("2");
        } else if (position == 2) {
            holder.image_ranking.setVisibility(View.VISIBLE);
            holder.image_ranking.setImageResource(R.mipmap.ranking_third_icon);
            holder.text_ranking1.setVisibility(View.VISIBLE);
            holder.text_ranking2.setVisibility(View.GONE);
            holder.text_ranking1.setText("3");
        } else {
            holder.image_ranking.setVisibility(View.INVISIBLE);
            holder.text_ranking1.setVisibility(View.GONE);
            holder.text_ranking2.setVisibility(View.VISIBLE);
            holder.text_ranking2.setText(String.valueOf(position + 1));
        }

        ImageLoader.getInstance()
                .displayImage(ApiDefine.IMAGE_URL_BASE + item.avatar, holder.image, LinhuApp.options);
        holder.text_name.setText(item.userName);
        holder.text_ranking_data.setText(item.problem_total);
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public ImageView image_ranking, image;
        public TextView text_ranking1, text_ranking2, text_name, text_ranking_data;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

            image_ranking = itemView.findViewById(R.id.image_ranking);
            image = itemView.findViewById(R.id.image);
            text_ranking1 = itemView.findViewById(R.id.text_ranking1);
            text_ranking2 = itemView.findViewById(R.id.text_ranking2);
            text_name = itemView.findViewById(R.id.text_name);
            text_ranking_data = itemView.findViewById(R.id.text_ranking_data);

        }

        @Override
        public void onClick(View v) {
        }
    }

}

